package com.cloudm.technician.ui.widget

import android.graphics.Rect
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.cloudm.technician.ui.modules.cloudm_box.upload_pic.UploadPicAdapter

/**
 * 作者: wesley
 * 时间: 2017/12/22
 * 描述: 自定义分隔线
 */
class SpaceItemDecoration(private val space: Int) : RecyclerView.ItemDecoration() {


    override fun getItemOffsets(outRect: Rect?, view: View?, parent: RecyclerView?,
                                state: RecyclerView.State?) {
        super.getItemOffsets(outRect, view, parent, state)

        val adapter = parent?.adapter
        val layoutManager = parent?.layoutManager

        if (adapter != null && adapter is UploadPicAdapter && layoutManager != null && layoutManager is GridLayoutManager) {
            val position = parent.getChildAdapterPosition(view)
            val spanCount = layoutManager.spanCount

            outRect?.top = space
            outRect?.bottom = 0
            when (position % spanCount) {
                0 -> {
                    outRect?.left = space
                    outRect?.right = space / 2
                }

                1 -> {
                    outRect?.left = space / 2
                    outRect?.right = space / 2
                }

                2 -> {
                    outRect?.right = space
                    outRect?.left = space / 2
                }
            }
        }
    }
}